package array;

import java.util.Arrays;

/**
 * @Author wangtengyu
 * @Date 2019/9/21 5:55 PM
 * @Description  获得一个数组的最小前缀
 */
public class CommonPrefix {



  public static void main(String args[]){
    String[] strArray=new String[]{"flower","flow","flight","a"};
    CommonPrefix commonPrefix=new CommonPrefix();
    System.out.println(commonPrefix.getCommonPrefix(strArray));
  }




  private String getCommonPrefix(String[] strs){

    //todo 检查合法字符串，懒得写了
    //todo 数组元素为1的也放弃了
    Arrays.sort(strs);
    int m=strs[0].length();
    int n=strs[strs.length-1].length();
    int max=Math.max(m,n);
    StringBuilder sb=new StringBuilder();
    for (int i=0;i<max;i++){
      if (strs[0].charAt(i)==strs[strs.length-1].charAt(i)){
        sb.append(strs[0].charAt(i));
      }else{
        break;
      }
    }
    return sb.toString();

  }




}
